package common;

import java.io.*;

/**
 * created by YT
 * description: 文件操作
 * User:lenovo
 * Data:2022-07-18
 * Time:8:53
 */
public class FileUtil {
    /**
    * @Author: YT摆渡人
    * @Description: 读写文件
    * @DateTime: 2022/7/18 8:56
    * @Params: 文件路径
    * @Return 文件内容
    */
    public static String readFile(String filePath) {
        StringBuilder stringBuilder = new StringBuilder();
        try (Reader reader = new FileReader(filePath)) {
            while (true) {
                int ch = reader.read();
                if (ch < 0) {
                    break;
                }
                stringBuilder.append((char) ch);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return stringBuilder.toString();
    }

    /**
    * @Author: YT摆渡人
    * @Description: 写入文件
    * @DateTime: 2022/7/18 8:56
    * @Params: 写入内容，写入路径
    * @Return
    */
    public static void writeFile(String filePath, String content) {
        try(Writer writer = new FileWriter(filePath)) {
            writer.write(content);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

}
